package com.zzh.lib.xpopup.ea;


import android.util.SparseArray;
import android.view.View;
import android.view.ViewGroup;

import com.zzh.lib.views.adapter.holder.HSuperViewHolder;
import com.zzh.lib.xpopup.interfaces.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Created by ZZH on 2022/12/8.
 *
 * @Date: 2022/12/8
 * @Email: zzh_hz@126.com
 * @QQ: 1299234582
 * @Author: zzh
 * @Description: 多类型适配器
 */
public class MultiItemTypeAdapter<T> extends RecyclerView.Adapter<HSuperViewHolder> {

    private SparseArray<View> mHeaderViews = new SparseArray<>();
    private SparseArray<View> mFootViews = new SparseArray<>();

    private List<T> data;

    public List<T> getData() {
        return data == null ? new ArrayList<>() : data;
    }

    public void setData(List<T> data) {
        this.data = data == null ? new ArrayList<>() : data;
    }

    public MultiItemTypeAdapter(List<T> data) {
        this.data = data == null ? new ArrayList<>() : data;
    }

    protected ItemDelegateManager<T> mItemDelegateManager = new ItemDelegateManager();
    protected OnItemClickListener mOnItemClickListener;

    public int getRealItemCount() {
        return getItemCount() - getHeadersCount() - getFootersCount();
    }


    public int getHeadersCount() {
        return mHeaderViews.size();
    }


    public int getFootersCount() {
        return mFootViews.size();
    }

    public int getItemViewType(int position) {
        if (isHeaderViewPos(position)) {
            return mHeaderViews.keyAt(position);
        } else if (isFooterViewPos(position)) {
            return mFootViews.keyAt(position - getHeadersCount() - getRealItemCount());
        }
        if (!useItemDelegateManager()) {
            return super.getItemViewType(position);
        } else {
            return mItemDelegateManager.getItemViewType(data.get(position - getHeadersCount()), position - getHeadersCount());
        }
    }


    public HSuperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mHeaderViews.get(viewType) != null) {
            return HSuperViewHolder.createViewHolder(mHeaderViews.get(viewType));

        } else if (mFootViews.get(viewType) != null) {
            return HSuperViewHolder.createViewHolder(mFootViews.get(viewType));
        }
        ItemDelegate<T> itemViewDelegate = mItemDelegateManager.getItemViewDelegate(viewType);

        int layoutId = itemViewDelegate.getLayoutId();
        HSuperViewHolder holder = HSuperViewHolder.createViewHolder(parent.getContext(), parent, layoutId);
        onViewHolderCreated(holder, holder.itemView);
        setListener(parent, holder, viewType);
        return holder;
    }

    public void onViewHolderCreated(HSuperViewHolder holder, View itemView) {
    }

    public void convert(HSuperViewHolder holder, T t, List<Object> payloads) {
        mItemDelegateManager.convert(holder, t, holder.getAdapterPosition() - getHeadersCount(), payloads);
    }

    protected boolean isEnabled(int viewType) {
        return true;
    }


    protected void setListener(ViewGroup parent, HSuperViewHolder viewHolder, int viewType) {
        if (!isEnabled(viewType)) return;
        viewHolder.itemView.setOnClickListener(v -> {
            if (mOnItemClickListener != null) {
                int position = viewHolder.getAdapterPosition() - getHeadersCount();
                mOnItemClickListener.onItemClick(v, viewHolder, position);
            }
        });


        viewHolder.itemView.setOnLongClickListener(v ->
        {
            if (mOnItemClickListener != null) {
                int position = viewHolder.getAdapterPosition() - getHeadersCount();
                return mOnItemClickListener.
                        onItemLongClick(v, viewHolder, position);
            }
            return false;
        });
    }

    public void onBindViewHolder(HSuperViewHolder holder, int position) {
        if (isHeaderViewPos(position)) {
            return;
        }
        if (isFooterViewPos(position)) {
            return;
        }
        convert(holder, data.get(position - getHeadersCount()), null);
    }

    public void onBindViewHolder(HSuperViewHolder holder, int position, List<Object> payloads) {
        if (isHeaderViewPos(position)) {
            return;
        }
        if (isFooterViewPos(position)) {
            return;
        }
        convert(holder, data.get(position - getHeadersCount()), payloads);
    }

    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);

        WrapperUtils.onAttachedToRecyclerView(recyclerView, new WrapperUtils.WrapperRecyclerViewAttachCallback() {
            @Override
            public int onAttached(GridLayoutManager layoutManager, GridLayoutManager.SpanSizeLookup oldLookup, int position) {

                int itemViewType = getItemViewType(position);
                if (mHeaderViews.get(itemViewType) != null) {
                    return layoutManager.getSpanCount();
                } else if (mFootViews.get(itemViewType) != null) {
                    return layoutManager.getSpanCount();
                }

                return oldLookup.getSpanSize(position);
            }
        });

    }

    public void onViewAttachedToWindow(HSuperViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        int position = holder.getLayoutPosition();
        if (isHeaderViewPos(position) || isFooterViewPos(position)) {
            WrapperUtils.setFullSpan(holder);
        }
    }

    public int getItemCount() {
        int itemCount = data.size();
        return getHeadersCount() + getFootersCount() + itemCount;
    }

    private boolean isHeaderViewPos(int position) {
        return position < getHeadersCount();
    }

    private boolean isFooterViewPos(int position) {
        return position >= getHeadersCount() + getRealItemCount();
    }

    public void addHeaderView(View view) {
        mHeaderViews.put(mHeaderViews.size() + BASE_ITEM_TYPE_HEADER, view);
    }

    public void addFootView(View view) {
        mFootViews.put(mFootViews.size() + BASE_ITEM_TYPE_FOOTER, view);
    }

    public MultiItemTypeAdapter<T> addItemDelegate(ItemDelegate<T> itemViewDelegate) {
        mItemDelegateManager.addDelegate(itemViewDelegate);
        return this;
    }

    public MultiItemTypeAdapter<T> addItemDelegate(int viewType, ItemDelegate<T> itemViewDelegate) {
        mItemDelegateManager.addDelegate(viewType, itemViewDelegate);
        return this;
    }

    protected boolean useItemDelegateManager() {
        return mItemDelegateManager.getItemViewDelegateCount() > 0;
    }


    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    private final int BASE_ITEM_TYPE_HEADER = 100000;
    private final int BASE_ITEM_TYPE_FOOTER = 200000;
}
